package com.burgeon.backend.EnterpriseWeChat.rest.vip;

import com.burgeon.backend.EnterpriseWeChat.entity.Vip;
import com.burgeon.backend.Rest.base.RESTBase;
import org.json.JSONArray;
import org.springframework.util.MultiValueMap;

import java.util.ArrayList;
import java.util.HashMap;

public class QueryVipByVipId extends RESTBase {
    private final static String RANGE = "20";//分页数量
    private String ip;
    private String account;
    private String password;

    public QueryVipByVipId(String ip, String account, String password) {
        this.ip = ip;
        this.account = account;
        this.password = password;
    }

    /**
     * 发送并接收字符创
     *
     * @return
     */

    public Vip getData(String cardNo) throws Exception {
        String url = "http://" + ip + "/servlets/binserv/Rest";

        String str = RESULT(Data(cardNo), url);
        System.err.println(str);
        String code = new JSONArray(str).getJSONObject(0).getString("code");
        if (!"0".equals(code)) {
            throw new Exception(new JSONArray(str).getJSONObject(0).getString("message"));
        }

        JSONArray jsonArray = new JSONArray(str).getJSONObject(0).getJSONArray("rows");
//        ArrayList<Vip> vipList = new ArrayList<>();
//        System.err.println(jsonArray.length());
        Vip vip = new Vip();
        if(jsonArray.length()>0){
                vip.setVipName(jsonArray.getJSONArray(0).getString(0));
                vip.setCardNo(jsonArray.getJSONArray(0).getString(1));
            if("null".equals(jsonArray.getJSONArray(0).getString(2))){
                vip.setLogoImg("http://lfwdcdn.cloudsmicro.com/img/EnterpriseWeChat/vip/app.png");
            }else {
                vip.setLogoImg(jsonArray.getJSONArray(0).getString(2));
            }
                vip.setOpenid(jsonArray.getJSONArray(0).getString(3));
                vip.setAmount(jsonArray.getJSONArray(0).getString(4));
                vip.setIntegral(jsonArray.getJSONArray(0).getString(5));
                vip.setId(jsonArray.getJSONArray(0).getString(6));
                vip.setEmpName(jsonArray.getJSONArray(0).getString(7));
                vip.setRank(jsonArray.getJSONArray(0).getString(8));
                vip.setStoreIdName(jsonArray.getJSONArray(0).getString(9));
            vip.setBirthday(jsonArray.getJSONArray(0).getString(10));
            vip.setAge(jsonArray.getJSONArray(0).getString(11));
            vip.setSex(jsonArray.getJSONArray(0).getString(12));
            vip.setPhone(jsonArray.getJSONArray(0).getString(13));
            vip.setOpenCardTime(jsonArray.getJSONArray(0).getString(14));
        }

        return vip;
    }

    private MultiValueMap Data(String cardNo) {
        HashMap params = new HashMap();
        ArrayList colums = new ArrayList();//查询字段

        colums.add("VIPNAME");//vip姓名
        colums.add("CARDNO");//卡号
        colums.add("LOGO_URL");//头像
//        colums.add("YC_8LABELDEFINE_ID");//累计购物金额
        colums.add("OPENID2");
        colums.add("AMOUNT");//储值余额
        colums.add("INTEGRAL");//积分
        colums.add("ID");
        colums.add("'HR_EMPLOYEE_ID1;NAME'");//专属导购
        colums.add("RANK");//等级
        colums.add("'C_STORE_ID;NAME'");
        colums.add("BIRTHDAY");
        colums.add("AGE");//年龄
        colums.add("SEX");//性别
        colums.add("MOBIL");//手机
        colums.add("OPENCARDDATE");

        params.put("columns", colums);
        params.put("table", "C_VIP");//表名

        params.put("count","true");

        HashMap paramss = new HashMap();
        paramss.put("column", "'CARDNO'");
        paramss.put("condition", cardNo);

        params.put("params", paramss);

        return DATA("Query", params, account, password);
    }

    public static void main(String[] args) {
        QueryVipByVipId g = new QueryVipByVipId("59.110.28.94:90", "nea@burgeon.com.cn", "burgeon55321");
        try {
            String empId = null;
            System.err.println(g.getData("1342816"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
